package tq.com.tqteacher.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import tq.com.systools.system.StringHelper;
import tq.com.tqteacher.R;

public class TQViewBottomInput extends FrameLayout {
    private ImageView mpz;
    private ImageView mlx;

    private TextView mSender;
    private EditText mInput;

    private int state = 1;

    public TQViewBottomInput(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initUi();
    }

    private void initUi(){
        LayoutInflater.from(getContext()).inflate(R.layout.view_liuyan_bottom, this, true);
        mpz = findViewById(R.id.pz);
        mlx = findViewById(R.id.lx);

        mSender = findViewById(R.id.sender);
        mInput = findViewById(R.id.input1);

        mlx.setOnClickListener(mOnClickListener);
        mSender.setOnClickListener(mOnClickListener);
        mpz.setOnClickListener(mOnClickListener);

        mInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "onTextChanged: ");
                if(s.length() > 0){
                    switchState(1);
                }else{
                    switchState(0);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        switchState(0);
    }

    private void switchState(int s){
        if(s == state) return;

        state = s;
        if(s == 0){
            mSender.setVisibility(GONE);
            mlx.setVisibility(VISIBLE);
        }else{
            mSender.setVisibility(VISIBLE);
            mlx.setVisibility(GONE);
        }
    }

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mISenderListener == null) return;
            if(v.getId() == R.id.sender){
                mISenderListener.senderText(StringHelper.editText(mInput));
                mInput.setText("");
            }else if(v.getId() == R.id.lx){
                mISenderListener.sendCatpture();
            }else {
                mISenderListener.senderPic();
            }
        }
    };
    public void setISenderListener(ISenderListener pSenderListener){
        mISenderListener = pSenderListener;
    }
    private ISenderListener mISenderListener;
    public interface ISenderListener{
        void senderText(String txt);
        void senderPic();

        void sendCatpture();
    }
    private String TAG = "TQViewBottomInput";
}
